package model

import (
	"food-mgr/food-service/configs"
	"gorm.io/gorm"
)

type Order struct {
	gorm.Model
	UserId   int     `gorm:"type:int(11);comment:用户ID"`
	ShopId   int     `gorm:"type:int(11);comment:商品ID"`
	BizId    int     `gorm:"type:int(11);comment:商家ID"`
	OrderSn  string  `gorm:"type:char(18);comment:订单号"`
	Address  string  `gorm:"type:varchar(30);comment:收货地址"`
	Quantity int     `gorm:"type:int(11);comment:购买数量"`
	Money    float64 `gorm:"type:decimal(10,2);comment:支付金额"`
	Status   string  `gorm:"type:varchar(30);comment:订单状态"`
	Year     string  `gorm:"type:varchar(50);comment:自提时间"`
	Number   string  `gorm:"type:varchar(30);comment:取件码"`
}

func CreateOrder(o *Order) error {

	return configs.DB.Create(&o).Error
}
func ListOrder(o *[]Order, page int, size int, BizId int64, userId int, status string) error {

	offset := (page - 1) * size
	return configs.DB.Where("biz_id=? or user_id=? or status=?", BizId, userId, status).Offset(offset).Limit(size).Order("Created_at DESC").Find(&o).Error
}
func UpdateOrder(o *Order, id int) {

	configs.DB.Where("id=?", id).Updates(&o)
}
func DelOrder(o *Order, id int) error {

	return configs.DB.Where("id=?", id).Delete(&o).Error
}
func FindOrder(o *Order, number string) {

	configs.DB.Where("number=?", number).First(&o)
}
